package com.bw.trade.monitor;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.core.creditStatus.RefundBorrowStatus;
import com.bw.core.vo.FindOneBankResVo;
import com.bw.core.vo.OrderRiskVO;
import com.bw.trade.fegin.FeginBillClient;
import com.bw.trade.fegin.FeginUserClient;
import com.bw.trade.mapper.TbBorrowLogMapper;
import com.bw.trade.pojo.TbBorrowLog;
import com.bw.trade.service.TbBorrowLogService;
import com.google.common.collect.Lists;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class MqServiceListener {

    @Autowired
    private Snowflake snowflake;
    @Autowired
    private TbBorrowLogMapper borrowLogMapper;
    @Autowired
    private FeginBillClient billClient;
    @Autowired
    private FeginUserClient userClient;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    private TbBorrowLogService tbBorrowLogService;
    //
    @RabbitHandler
    @RabbitListener(queues = "item_queue")
    public void getMsg(String msg) {

        JSONObject jsonObject = JSONObject.parseObject(msg);
        OrderRiskVO orderRiskVO = JSONObject.toJavaObject(jsonObject, OrderRiskVO.class);
        String orderNo = orderRiskVO.getOrderNo();

        //判断订单是否为空
        if(StrUtil.hasBlank(orderNo)){
            return;
        }
        //根据订单编号和状态查询是否存在订单
        QueryWrapper<TbBorrowLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo)
                    .eq("borrow_status", Lists.newArrayList(1,2));

       //查询数据是否存在
        List<TbBorrowLog> list = borrowLogMapper.selectList(queryWrapper);
        if(!CollectionUtil.isEmpty(list)){
            return;
        }

        TbBorrowLog borrowLog = new TbBorrowLog();
        borrowLog.setOrderNo(orderRiskVO.getOrderNo());//订单订单编号
        borrowLog.setBorrowStatus(RefundBorrowStatus.REFUND_BORROW_CHECK_RUNNING);//处理状态
        borrowLog.setTransactionNo(snowflake.nextId() + "");
        borrowLog.setIsDelete(0);
        borrowLog.setCreateTime(new Date());
        borrowLog.setUpdateTime(new Date());
        borrowLog.setUserId(orderRiskVO.getUserId());
        borrowLog.setUserName(orderRiskVO.getUserName());
        borrowLog.setPayAmount(orderRiskVO.getOrderApplyMoney());
        //feign调用获取银行卡信息对象
        FindOneBankResVo bank = userClient.findBankResVo(orderRiskVO.getUserId());
        borrowLog.setBankName(bank.getBandName());
        borrowLog.setBankShortName(bank.getBankShortName());
        borrowLog.setTargetAccount(bank.getBankCardNo());

        borrowLogMapper.insert(borrowLog);

        //调用第三方接口
        tbBorrowLogService.callMethod(orderRiskVO,borrowLog);

    }

}